package nirvana.core.bean.json.annotationIntrospector;

import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import lombok.extern.slf4j.Slf4j;
import nirvana.core.bean.json.file.filePath.OssFilePathDeserializer;
import nirvana.core.bean.json.file.filePath.OssFilePathSerializer;
import nirvana.core.domains.ann.FilePath;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * 【描 述】：
 * 【环 境】：J2SE 11
 * 【详 细】：
 *
 * @author 李松林
 * @version v1.0 2023-02-01 新建
 * @since 2023-02-01
 */
@Slf4j
@Component
@ConditionalOnProperty(value = "fileStorageModel",havingValue = "oss")
public class FilePathAnnotationIntrospector extends NopAnnotationIntrospector {

    private final OssFilePathSerializer ossFilePathSerializer;

    private final OssFilePathDeserializer ossFilePathDeserializer;


    public FilePathAnnotationIntrospector(OssFilePathSerializer ossFilePathSerializer, OssFilePathDeserializer ossFilePathDeserializer) {
        this.ossFilePathSerializer = ossFilePathSerializer;
        this.ossFilePathDeserializer = ossFilePathDeserializer;
    }


    /**
     * 如果字段有 @FilePath 注解则进行特殊序列化
     * @param am
     * @return
     */
    @Override
    public Object findSerializer(Annotated am) {
        FilePath annotation = am.getAnnotation(FilePath.class);
        if (annotation!=null){
            return ossFilePathSerializer;
        }
        return null;
    }


    @Override
    public Object findDeserializer(Annotated am) {
        FilePath annotation = am.getAnnotation(FilePath.class);
        if (annotation!=null){
            return ossFilePathDeserializer;
        }
        return null;
    }
}
